<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>PHP 7 错误处理</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.errors.basics.html">« 基础概要</a></li>
      <li style="float: right;"><a href="language.exceptions.html">异常 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.errors.html">错误</a></li>
    <li>PHP 7 错误处理</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.errors.php7" class="sect1">
 <h2 class="title">PHP 7 错误处理</h2>

 <p class="para">
  PHP 7 改变了大多数错误的报告方式。不同于传统（PHP 5）的错误报告机制，现在大多数错误被作为
  <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 异常抛出。
 </p>

 <p class="para">
  这种 <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 异常可以像 <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span>
  异常一样被第一个匹配的 <code class="literal">try</code> / <code class="literal">catch</code>
  块所捕获。如果没有匹配的
  <a href="" class="link"><code class="literal">catch</code></a>
  块，则调用异常处理函数（事先通过 <span class="function"><a href="function.set-exception-handler.html" class="function">set_exception_handler()</a></span> 注册）进行处理。
  如果尚未注册异常处理函数，则按照传统方式处理：被报告为一个致命错误（Fatal Error）。
 </p>

 <p class="para">
  <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 类并非继承自 <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span>
  类，所以不能用 <code class="code">catch (Exception $e) { ... }</code> 来捕获
  <span class="classname"><a href="class.error.html" class="classname">Error</a></span>。你可以用
  <code class="code">catch (Error $e) { ... }</code>，或者通过注册异常处理函数（
  <span class="function"><a href="function.set-exception-handler.html" class="function">set_exception_handler()</a></span>）来捕获 <span class="classname"><a href="class.error.html" class="classname">Error</a></span>。
 </p>

 <div class="sect2" id="language.errors.php7.hierarchy">
  <h3 class="title"><span class="classname"><a href="class.error.html" class="classname">Error</a></span> 层次结构</h3>

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara"><span class="classname"><a href="class.throwable.html" class="classname">Throwable</a></span></span>
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara"><span class="classname"><a href="class.error.html" class="classname">Error</a></span></span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.arithmeticerror.html" class="classname">ArithmeticError</a></span></span>
        <ul class="itemizedlist">
         <li class="listitem">
          <span class="simpara"><span class="classname"><a href="class.divisionbyzeroerror.html" class="classname">DivisionByZeroError</a></span></span>
         </li>
        </ul>
       </li>
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.assertionerror.html" class="classname">AssertionError</a></span></span>
       </li>
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.compileerror.html" class="classname">CompileError</a></span></span>
        <ul class="itemizedlist">
         <li class="listitem">
          <span class="simpara"><span class="classname"><a href="class.parseerror.html" class="classname">ParseError</a></span></span>
         </li>
        </ul>
       </li>
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span></span>
        <ul class="itemizedlist">
         <li class="listitem">
          <span class="simpara"><span class="classname"><a href="class.argumentcounterror.html" class="classname">ArgumentCountError</a></span></span>
         </li>
        </ul>
       </li>
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span></span>
       </li>
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.unhandledmatcherror.html" class="classname">UnhandledMatchError</a></span></span>
       </li>
       <li class="listitem">
        <span class="simpara"><span class="classname"><a href="class.fibererror.html" class="classname">FiberError</a></span></span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara"><span class="classname"><a href="class.exception.html" class="classname">Exception</a></span></span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">...</span>
       </li>
      </ul>
     </li>
    </ul>
   </li>
  </ul>
 </div>
</div></div></div></body></html>